<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="超市名称" prop="supermarketId">
        <el-select v-model="queryParams.supermarketId" @keyup.enter.native="handleQuery" placeholder="请选择超市">
                <el-option
                  v-for="item in supermarketOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
      </el-form-item>
      <el-form-item>
      <el-select
              v-model="queryParams.workStatus"
              placeholder="工作状态"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_work_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
      <el-form-item label="员工姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
  
      <el-form-item label="身份证号码" label-width="90px" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="居住地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入居住地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="postId">
        <el-select v-model="queryParams.postId" placeholder="请选择岗位">
                <el-option
                  v-for="item in postOptions"
                  :key="item.postId"
                  :label="item.postName"
                  :value="item.postId"
                ></el-option>
              </el-select>
      </el-form-item>
     
      <el-form-item label="入职时间" prop="entryTime">
        <el-date-picker clearable
          v-model="queryParams.entryTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入职时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supermarket:employee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supermarket:employee:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['supermarket:employee:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supermarket:employee:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employeeList" :fit="true" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="员工编号" align="center" prop="id" v-if="columns[0].visible" />
      <el-table-column label="超市" align="center" prop="supermarketName" v-if="columns[1].visible"/>
      <el-table-column label="姓名" align="center" prop="name" v-if="columns[2].visible"/>
      <el-table-column label="岗位" align="center" prop="postName" v-if="columns[12].visible"/>
      <el-table-column label="工作照" align="center" prop="avatar" v-if="columns[13].visible">
        <template slot-scope="scopephoto">
          <image-preview style="width:80px;height:80px;" :src="scopephoto.row.avatar"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" v-if="columns[3].visible"/>
      <el-table-column label="性别" align="center" prop="sex" v-if="columns[4].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
        </el-table-column>
      <el-table-column label="身份证号码" align="center" width="180px" prop="idCard" v-if="columns[6].visible"/>
      <el-table-column label="居住地址" align="center" prop="address" v-if="columns[7].visible"/>
      <el-table-column label="联系电话" align="center" width="110px" prop="phone" v-if="columns[8].visible"/>
      <el-table-column label="工龄" align="center" prop="seniority" v-if="columns[9].visible"/>
      <el-table-column label="薪水/元" align="center" prop="salary" v-if="columns[5].visible"/>
    
      <el-table-column label="工作状态" align="center" prop="workStatus" v-if="columns[14].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_work_type" :value="scope.row.workStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="入职时间" align="center" prop="entryTime" width="180" v-if="columns[10].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="80px">
        <el-form-item label="超市" prop="supermarketId">
          <el-select v-model="form.supermarketId" placeholder="请选择超市">
                <el-option
                  v-for="item in supermarketOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="岗位" prop="postId">
          <el-select v-model="form.postId" placeholder="请选择岗位">
                <el-option
                  v-for="item in postOptions"
                  :key="item.postId"
                  :label="item.postName"
                  :value="item.postId"
                ></el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="性别">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" type="number"  placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="薪水" prop="salary">
          <el-input v-model="form.salary" type="number" placeholder="请输入薪水" />
        </el-form-item>
        <el-form-item label="身份证号码" label-width="90px" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号码" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入居住地址" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="状态">
              <el-radio-group v-model="form.workStatus">
                <el-radio
                  v-for="dict in dict.type.sys_work_type"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
        <el-form-item label="入职时间" prop="entryTime">
          <el-date-picker clearable
            v-model="form.entryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入职时间">
          </el-date-picker>
        </el-form-item>
        
        
        <el-form-item label="工作照" label-width="90px" prop="avatar">
          <image-upload  v-model="form.avatar" :limit="1"/>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmployee, getEmployee, delEmployee, addEmployee, updateEmployee } from "@/api/supermarket/employee";
import {getSupermarketName} from "@/api/supermarket/supermarket";
import {listPostName} from "@/api/system/post";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Employee",
  dicts: ['sys_normal_disable', 'sys_user_sex','sys_work_type'],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

       //超市选项
       supermarketOptions: [],

       //岗位选项
       postOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supermarketId: null,
        workStatus:null,
        name: null,
        age: null,
        sex: null,
        salary: null,
        idCard: null,
        address: null,
        phone: null,
        seniority: null,
        entryTime: null,
        postId: null,
        avatar: null,
        workStatus: null,
        remarks: null
      },
      // 表单参数
      form: {},
      columns: [
        { key: 0, label: `员工编号`, visible: true },
        { key: 1, label: `超市名称`, visible: true },
        { key: 2, label: `员工姓名`, visible: true },
        { key: 3, label: `年龄`, visible: true },
        { key: 4, label: `性别`, visible: true },
        { key: 5, label: `薪水`, visible: true },
        { key: 6, label: `身份证号码`, visible: true },
        { key: 7, label: `居住地址`, visible: true },
        { key: 8, label: `联系电话`, visible: true },
        { key: 9, label: `工龄`, visible: true },
        { key: 10, label: `入职时间`, visible: true },
        { key: 11, label: `部门名称`, visible: true },
        { key: 12, label: `职位名称`, visible: true },
        { key: 13, label: `工作照`, visible: true },
        { key: 14, label: `工作状态`, visible: true },
        { key: 15, label: `备注`, visible: true }

      ],
      // 表单校验
      rules: {
        supermarketId: [
          { required: true, message: "超市名称不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "员工姓名不能为空", trigger: "blur" }
        ],
        idCard:{required: true, message: "身份证不能为空", trigger: "blur"},
        postId: {required: true, message: "岗位不能为空", trigger: "blur"},
        address: {required: true, message: "地址不能为空", trigger: "blur"},
        entryTime: {required: true, message: "入职时间不能为空", trigger: "blur"},
        workStatus: {required: true, message: "工作状态不能为空", trigger: "blur"},
        salary: {required: true, message: "薪水不能为空", trigger: "blur"},



      }
    };
  },
  created() {
    this.getSupermarketNameList();
    this.getList();
  },
  methods: {
    /** 查询员工列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then(response => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supermarketId: null,
        name: null,
        age: null,
        sex: null,
        salary: null,
        idCard: null,
        address: null,
        phone: null,
        seniority: null,
        entryTime: null,
        postId: null,
        deptId: null,
        avatar: null,
        delFlag: null,
        workStatus: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
     // 初始化超市名称
     getSupermarketNameList(){
      if(this.$store.getters.userId == 0){
        return;
      }
      const userId =this.$store.getters.userId;
      getSupermarketName(userId).then(response => {
        this.supermarketOptions =response.data;
    });
      listPostName().then(response => {
        this.postOptions =response.data;
    });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEmployee(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEmployee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmployee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工编号为"' + ids + '"的数据项？').then(function() {
        return delEmployee(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/employee/export', {
        ...this.queryParams
      }, `employee_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
